/*
 * Created by JFormDesigner on Thu Jul 15 12:43:14 CST 2021
 */

package com.minelx.statementplus.ui.form.ui.memberbased.addingchildsql;

import com.minelx.sequence.common.Templates;
import com.minelx.statementplus.ui.form.dao.BaseListModel;
import com.minelx.statementplus.ui.form.service.tablemapping.ITableMapping;
import com.minelx.statementplus.ui.form.swing.OnClosingDialog;
import com.minelx.statementplus.ui.form.vo.ChildSqlBuilderPairValue;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.util.Map;

import static com.minelx.statementplus.ui.form.swing.DialogInputs.candyOrJoke;
import static java.util.stream.Collectors.toMap;

/**
 * @author aaa
 */
public class AddingChildSqlDialog extends OnClosingDialog {

	private final ITableMapping mapping;

	private final BaseListModel<ChildSqlBuilderPairValue> childSqlListModel;

	public AddingChildSqlDialog(Window owner,
	                            BaseListModel<ChildSqlBuilderPairValue> childSqlListModel,
	                            ITableMapping mapping) {
		super(owner, e -> {});
		this.childSqlListModel = childSqlListModel;
		this.mapping = mapping;
		initComponents();

		childrenSqlList.setModel(childSqlListModel);

		DefaultListCellRenderer defaultListCellRenderer = new DefaultListCellRenderer();
		childrenSqlList.setCellRenderer((list, value, index, isSelected, cellHasFocus) ->
				defaultListCellRenderer.getListCellRendererComponent(list,
						value.name(),
						index,
						isSelected,
						cellHasFocus));
	}

	private void addSqlButtonActionPerformed(ActionEvent e) {
		new ChoosingChildSqlDialog(this, this::addChildSql, mapping).setVisible(true);
	}

	private void removeSqlButtonActionPerformed(ActionEvent e) {
		childSqlListModel.removeElement(childrenSqlList.getSelectedIndex());
	}

	private void clearButtonActionPerformed(ActionEvent e) {
		childSqlListModel.clear();
	}

	private void addChildSql(ChildSqlBuilderPairValue added) {
		Templates templates = new Templates(added.childSql());
		Map<String, String> parameters = templates.variables().stream()
				.collect(toMap(variableName -> variableName, this::valueOfVariable));

		String name = added.name() + ", parameters: " + parameters;
		childSqlListModel.addElement(new ChildSqlBuilderPairValue(name, templates.render(parameters)));
	}

	private String valueOfVariable(String variableName) {
		return candyOrJoke(this, variableName);
	}

	private void initComponents() {
		// JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
		panel2 = new JPanel();
		scrollPane1 = new JScrollPane();
		childrenSqlList = new JList<>();
		panel1 = new JPanel();
		addSqlButton = new JButton();
		removeSqlButton = new JButton();
		clearButton = new JButton();
		label1 = new JLabel();

		//======== this ========
		setTitle("\u81ea\u5b9a\u4e49\u67e5\u8be2");
		setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		var contentPane = getContentPane();
		contentPane.setLayout(new BorderLayout());

		//======== panel2 ========
		{
			panel2.setBorder(new EmptyBorder(12, 12, 12, 12));
			panel2.setLayout(new BorderLayout());

			//======== scrollPane1 ========
			{

				//---- childrenSqlList ----
				childrenSqlList.setFont(new Font("Microsoft JhengHei Light", Font.PLAIN, 20));
				scrollPane1.setViewportView(childrenSqlList);
			}
			panel2.add(scrollPane1, BorderLayout.CENTER);

			//======== panel1 ========
			{
				panel1.setLayout(new FlowLayout(FlowLayout.TRAILING));

				//---- addSqlButton ----
				addSqlButton.setText("\u6dfb\u52a0");
				addSqlButton.setFont(new Font("Microsoft JhengHei Light", Font.PLAIN, 18));
				addSqlButton.addActionListener(e -> addSqlButtonActionPerformed(e));
				panel1.add(addSqlButton);

				//---- removeSqlButton ----
				removeSqlButton.setText("\u5220\u9664");
				removeSqlButton.setFont(new Font("Microsoft JhengHei Light", Font.PLAIN, 18));
				removeSqlButton.addActionListener(e -> removeSqlButtonActionPerformed(e));
				panel1.add(removeSqlButton);

				//---- clearButton ----
				clearButton.setText("\u6e05\u7a7a");
				clearButton.setFont(new Font("Microsoft JhengHei Light", Font.PLAIN, 18));
				clearButton.addActionListener(e -> clearButtonActionPerformed(e));
				panel1.add(clearButton);
			}
			panel2.add(panel1, BorderLayout.SOUTH);

			//---- label1 ----
			label1.setText("  \u5df2\u6dfb\u52a0\u7684\u67e5\u8be2");
			label1.setFont(new Font("Microsoft JhengHei Light", Font.PLAIN, 17));
			panel2.add(label1, BorderLayout.NORTH);
		}
		contentPane.add(panel2, BorderLayout.CENTER);
		setSize(655, 480);
		setLocationRelativeTo(getOwner());
		// JFormDesigner - End of component initialization  //GEN-END:initComponents
	}

	// JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
	private JPanel panel2;
	private JScrollPane scrollPane1;
	private JList<ChildSqlBuilderPairValue> childrenSqlList;
	private JPanel panel1;
	private JButton addSqlButton;
	private JButton removeSqlButton;
	private JButton clearButton;
	private JLabel label1;
	// JFormDesigner - End of variables declaration  //GEN-END:variables
}
